﻿using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using com.pro2e.console.core.valueobject;
using NHibernate.Criterion;
using com.pro2e.console.service.core;
using com.pro2e.console.generic.util;
using com.pro2e.console.core.hibernate;
using System.Collections;
using System;
using com.pro2e.web.config;
using System.Text.RegularExpressions;


/// <summary>
/// 提供一般共用Service
/// </summary>
namespace sexhealth.console.service.common
{
    public class CommonService : ServiceImpl
    {

        public CommonService()
        {

        }
        public static string WrapImg(string fileName)
        {
            ConfigHelper configHelper = new ConfigHelper();
            return "<img src='http://" + configHelper.Host + "/upload/pic/" + fileName + "' border='0' width='50px' height='50px' />";
        }
        /// <summary>
        /// 轉成10碼格式(0000-000-000)
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public static string MobileFormat(string phoneNumber)
        {
            if (phoneNumber.Length == 10)
            {
                return int.Parse(phoneNumber).ToString("0###-###-###");
            }
            else
            {
                return phoneNumber;
            }
        }
        /// <summary>
        /// 取得中文的星期幾
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string GetChineseDayOfWeek(DateTime dateTime)
        {
            string result;
            result = System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames[(byte)dateTime.DayOfWeek];
            return result.Replace("星期", string.Empty);
        }

        /// <summary>
        /// 轉成新台幣格式
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public static string CurrencyFormat(decimal currency)
        {
            return string.Format("{0:#,##0}", currency);
        }
        /// <summary>
        /// 轉成新台幣格式
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public static string CurrencyFormat(int currency)
        {
            return string.Format("{0:#,##0}", currency);
        }






        /// <summary>
        /// 將IList轉DataTable
        /// </summary>
        /// <param name="i_objlist"></param>
        /// <returns></returns>
        public static DataTable ConvertToDataTable<T>(IList<T> i_objlist)
        {
            if (i_objlist == null || i_objlist.Count <= 0)
            {
                return null;
            }
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;

            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            foreach (T t in i_objlist)
            {
                if (t == null)
                {
                    continue;
                }

                row = dt.NewRow();

                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];

                    string name = pi.Name;

                    if (dt.Columns[name] == null)
                    {
                        //modify @ 2009/09/14 brian
                        column = new DataColumn(name, name.GetType());
                        dt.Columns.Add(column);
                    }

                    row[name] = pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }
            // Add the id column as a primary key
            DataColumn[] keys = new DataColumn[1];
            keys[0] = dt.Columns[0];
            dt.PrimaryKey = keys;

            return dt;
        }
        /// <summary>
        /// 將List轉DataTable
        /// </summary>
        /// <param name="i_objlist"></param>
        /// <returns></returns>
        public static DataTable ConvertToDataTable<T>(List<T> i_objlist)
        {
            if (i_objlist == null || i_objlist.Count <= 0)
            {
                return null;
            }
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;

            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            foreach (T t in i_objlist)
            {
                if (t == null)
                {
                    continue;
                }

                row = dt.NewRow();

                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];

                    string name = pi.Name;

                    if (dt.Columns[name] == null)
                    {
                        //modify @ 2009/09/14 brian
                        column = new DataColumn(name, name.GetType());
                        dt.Columns.Add(column);
                    }

                    row[name] = pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }
            // Add the id column as a primary key
            DataColumn[] keys = new DataColumn[1];
            keys[0] = dt.Columns[0];
            dt.PrimaryKey = keys;

            return dt;
        }

        /// <summary>
        /// 轉成民國年
        /// </summary>
        /// <param name="dateToConvert"></param>
        /// <returns></returns>
        public static DateTime ConvertToTaiwanCalendar(DateTime dateToConvert)
        {

            System.Globalization.TaiwanCalendar twC = new System.Globalization.TaiwanCalendar();
            return DateTime.Parse(twC.GetYear(dateToConvert) + dateToConvert.ToString("/MM/dd"));
        }
        public static string LocalLanguageDayOfWeek(DateTime dateToConvert)
        {
            return System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames[(byte)dateToConvert.DayOfWeek];
        }
        /// <summary>
        /// 轉成民國年 II
        /// </summary>
        public static string ConvertToTaiwanCalendar2(string str)
        {
            string year = str.Split(new char[] { '/' })[0];
            string month = str.Split(new char[] { '/' })[1];
            string day = str.Split(new char[] { '/' })[2];
            int intYear;
            if (int.TryParse(year, out intYear))
            {
                year = intYear.ToString("0000");
            }

            return year + "/" + month + "/" + day;
        }
        public void MergeCells(GridView gv, int cellToMerge, int cellToFollow)
        {
            int i = 1;
            foreach (GridViewRow wkItem in gv.Rows)
            {
                if (wkItem.RowIndex != 0)
                {

                    if (wkItem.Cells[cellToFollow].Text.Trim() == gv.Rows[(wkItem.RowIndex - i)].Cells[cellToFollow].Text.Trim())
                    {
                        gv.Rows[(wkItem.RowIndex - i)].Cells[cellToMerge].RowSpan += 1;
                        wkItem.Cells[cellToMerge].Visible = false;
                        i = i + 1;
                    }
                    else
                    {
                        gv.Rows[(wkItem.RowIndex)].Cells[cellToMerge].RowSpan += 1;
                        i = 1;
                    }
                }
                else
                {
                    wkItem.Cells[cellToMerge].RowSpan = 1;
                }
            }
        }
        public static string GetPureContent(string Htmlstring)
        {
            //删除脚本 
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML 
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

            return Htmlstring;
        }
        /// <summary>
        /// ILIST 轉 Dateset
        /// </summary>
        public static DataSet ConvertToDataSet<T>(IList<T> list)
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }

            DataSet ds = new DataSet();
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;

            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }

                row = dt.NewRow();

                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];

                    string name = pi.Name;

                    if (dt.Columns[name] == null)
                    {
                        //column = new DataColumn(name, pi.PropertyType);
                        //modify @ 2009/09/14 brian
                        column = new DataColumn(name, name.GetType());
                        dt.Columns.Add(column);
                    }

                    row[name] = pi.GetValue(t, null);
                }

                dt.Rows.Add(row);
            }

            ds.Tables.Add(dt);

            return ds;
        }



        public static string ExtractFileName(string filePath)
        {
            filePath = filePath.Substring(filePath.LastIndexOf('\\')+1).Trim();
            return filePath;
        }
    }
}